//===================================
//File: Point.cpp
//Description class Point
//Name: Smirnova O.P.
//Date: 01.07.11, v.1.0
//===================================

#define M_PI 3.141527
#include "Point.h"

using namespace std;

Point::Point(float f1,float f2,short isPolar)

{
  if(isPolar==1)
  {

    this->r = f1;
    this->fi = f2;
    convertToDecart();
  }
  else
  {
    this->x = f1;
    this->y = f2;
    convertToPolar();
  }
 // cout << endl
   //    << "Point:" << endl
  //     << "Object of class Point with"
      // << endl   << "x="
    //   << getX() << " y=" << getY()
      // << endl   << "r="
      // << getR() << " fi=" << getFI()
      // << endl
      // << "Point." << endl;
       //	cout << endl << "Press enter please";
       //	getch();
}

Point::~Point()
{
/*  cout << endl
       << "~Point:" << endl
       << "Object of class Point with"
       << endl << "x="
       << getX() << " y=" << getY()
       << endl << "r="
       << getR() << " fi=" << getFI()
       << endl
       << "~Point." << endl;

*/
}

float Point::getX()
{
  return x;
}

float Point::getY()
{
  return y;
}

float Point::getR()
{
  return r;
}

float Point::getFI()
{
  return fi;
}

void Point::setR(float r)
{
 this->r = r;
 convertToDecart();
}

void Point::setFI(float fi)
{
 if(fi>=2*M_PI)
 this->fi = fi-2*M_PI;
 else this->fi = fi;
 convertToDecart();
}

void Point::convertToPolar()
{
  r = sqrt(x*x+y*y);
  if(x>0 && y>=0) fi = atan(y/x);
  else if(x>0 && y<0) fi = atan(y/x)+2*M_PI;
  else if(x<0) fi = atan(y/x)+M_PI;
  else if(x==0 && y>0) fi = M_PI/2;
  else if(x==0 && y<0) fi = 3*M_PI/2;
}

void Point::convertToDecart()
{
  x = r*cos(fi);
  y = r*sin(fi);
}

void Point::moveToPoint(Point destP)
{
  this->x = destP.getX();
  this->y = destP.getY();
  this->r = destP.getR();
  this->fi = destP.getFI();
}
void Point::writePoint()
{
  cout << endl
       << "Current Point:"
       << endl
       << "Decart: x="
       << x
       << " , y="
       << y
       << endl
       << "Polar: r="
       << r
       << ", fi="
       << fi
       << endl;


}
void Point::consSP(Point &SP)
{
  float nX,nY;
  nX = SP.getX();
  nY = SP.getY();
  SP.x=(x-nX);
  SP.y=(y-nY);
  SP.convertToPolar();
  SP.setFI(SP.getFI()+M_PI);
  SP.x=(SP.getX()+nX);
  SP.y=(SP.getY()+nY);
  SP.convertToPolar();
}

void Point::moveTodXdY(double dx,double dy)
{
  this->x += dx;
  this->y += dy;
  convertToPolar();
}

int Point::operator>(int e)
{
	if(e==0)
	{
		if(this->getX()>e && this->getY()>e) return 1;
		else return 0;
	}
	return 0;
}

ostream& operator<<(ostream& cout,Point p)
{
	  cout << endl
       << "Current Point:"
       << endl
       << "Decart: x="
	   << p.getX()
       << " , y="
       << p.getY()
       << endl
       << "Polar: r="
       << p.getR()
       << ", fi="
       << p.getFI()
       << endl;
	return cout;
}













